package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.feign.BehaviorFeign;
import com.usian.article.feign.UserFeign;
import com.usian.article.mapper.ApArticleConfigMapper;
import com.usian.article.mapper.ApArticleContentMapper;
import com.usian.article.mapper.ApAuthorMapper;
import com.usian.article.mapper.ApCollectionMapper;
import com.usian.article.service.ApCollectionService;
import com.usian.article.service.ArticleInfoService;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.article.pojos.ApCollection;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApLikesBehavior;
import com.usian.model.behavior.pojos.ApUnlikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ArticleInfoServiceImpl implements ArticleInfoService {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private BehaviorFeign behaviorFeign;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private ApCollectionMapper apCollectionMapper;

    @Autowired
    private ApAuthorMapper apAuthorMapper;

    @Override/*查看ap端文章详细 就是查ap端文章的内容和配置*/
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {

        Map<String ,Object> map = new HashMap<>();

        if (dto == null || dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        /*先查看ap端文章配置表 看看 是否删除 或者是否上下架了*/
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));

        if (apArticleConfig==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        /*相当于就是没有删除 没有下架*/
        if (!apArticleConfig.getIsDelete() && apArticleConfig.getIsDown()){
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));

            map.put("content",apArticleContent);

        }

        map.put("config",apArticleConfig);

        return ResponseResult.okResult(map);
    }

    @Override/*加载文章详情的初始化配置信息，比如关注、喜欢、不喜欢等*/
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {

        /*检验参数*/
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        /*获取用户id*/
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户不存在");
        }
        /*查询实体*/
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findApBehaviorEntry(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"实体不存在");
        }

        /*定义状态  不喜欢(type:0 不喜欢    1 取消不喜欢)
                    点赞(type:0 文章     1 动态   operation：0 点赞      1 取消点赞)
                    收藏(type：0 文章     1 动态)
                    关注()
        */
        boolean isUnlike=false,isLike = false,isCollection = false,isFollow = false;

        /*查询不喜欢   根据实体id和文章id*/
        ApUnlikesBehavior unlikesBehavior = behaviorFeign.findUnLikeByArticleIdAndEntryId(apBehaviorEntry.getId(), dto.getArticleId());
        if (unlikesBehavior!=null && unlikesBehavior.getType()==0){
            isUnlike=true;
        }

        /*查询点赞 根据实体id和文章id和文章点赞类型*/
        ApLikesBehavior likesBehavior = behaviorFeign.findLikeByArticleIdAndEntryId(dto.getArticleId(), apBehaviorEntry.getId(), (short) 0);
        if (likesBehavior!=null && likesBehavior.getOperation()==0){
            isLike=true;
        }

        /*查询收藏*/
        ApCollection apCollection = apCollectionMapper.selectOne(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getEntryId, apBehaviorEntry.getId())
                .eq(ApCollection::getArticleId, dto.getArticleId()).eq(ApCollection::getType,(short)0));

        if (apCollection!=null){
            isCollection=true;
        }

        /*查询关注 apUserFollow   根据用户id和作者id查   所以先查作者*/
        ApAuthor apAuthor = apAuthorMapper.selectById(dto.getAuthorId());
        if (apAuthor!=null){
            ApUserFollow UserFollow = userFeign.findByUserIdAndFollowId(user.getId(), apAuthor.getId());
            if (UserFollow!=null){
                isFollow=true;
            }
        }

        //7.结果返回  {"isfollow":true,"islike":true,"isunlike":false,"iscollection":true}
        Map<String,Object> result =new HashMap<>();
        result.put("isLike",isLike);
        result.put("isUnlike",isUnlike);
        result.put("isCollection",isCollection);
        result.put("isFollow",isFollow);
        return ResponseResult.okResult(result);
    }
}
